package cn.adelyn.blog.blogmanager.controller;

import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.blogmanager.pojo.dto.InsertTagDTO;
import cn.adelyn.blog.blogmanager.pojo.dto.TagSearchDTO;
import cn.adelyn.blog.blogmanager.pojo.dto.UpdateTagDTO;
import cn.adelyn.blog.blogmanager.pojo.vo.TagVO;
import cn.adelyn.blog.blogmanager.service.TagService;
import cn.adelyn.common.core.pojo.vo.PageVO;
import cn.adelyn.common.core.response.ServerResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotNull;

@Validated
@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    TagService tagService;

    @PostMapping
    public ServerResponseEntity insertTag(@RequestBody @Validated InsertTagDTO insertTagDTO) {
        tagService.insertTag(insertTagDTO.getTagName(), AuthUserContext.get().getUserId());
        return ServerResponseEntity.success();
    }

    @PutMapping
    public ServerResponseEntity updateTag(@RequestBody @Validated UpdateTagDTO updateTagDTO) {
        tagService.updateTag(updateTagDTO.getTagId(), updateTagDTO.getTagName());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    public ServerResponseEntity deleteTag(@NotNull Long tagId) {
        tagService.deleteTag(tagId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/page")
    public ServerResponseEntity<PageVO<TagVO>> getTagPage(@RequestBody @Validated TagSearchDTO tagSearchDTO) {
        return ServerResponseEntity.success(
                tagService.getTagPage(AuthUserContext.get().getUserId(), tagSearchDTO.getPageDTO())
        );
    }
}
